package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import extend.org.compiere.utils.Util;

public class Mhrmattendance extends X_hrm_attendance {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6124601486962487864L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmattendance.class);

	public Mhrmattendance(Properties ctx, int hrm_attendance_ID, String trxName) {
		super(ctx, hrm_attendance_ID, trxName);
	}

	public Mhrmattendance(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
	}
	
	@Override
	protected boolean beforeSave(boolean newRecord) {
		
		// Error message
		StringBuilder errmsg = new StringBuilder();
	
		if(gethrm_hourquota().intValue() == 0)
			errmsg.append(Util.requiredField(COLUMNNAME_hrm_hourquota)).append("\n");
		
		Util.checkErrMsg(errmsg);
		
		return true;
	}
	
	public static Mhrmattendance getAttendanceRec(int C_BPartner_ID, int hrm_order_ID){
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmattendance result = null;
			
		// 
		String sql_ = "SELECT * FROM HRM_ATTENDANCE " +
						"WHERE ISACTIVE='Y' \n" +
							"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n" + 
							"AND C_BPARTNER_ID=" + C_BPartner_ID + " \n" +
							"AND HRM_ORDER_ID=" + hrm_order_ID   + " \n" ;
			try
			{
				pstmt = DB.prepareStatement(sql_,null);				
				rs = pstmt.executeQuery();	
				if(rs.next())
					result = new Mhrmattendance(Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}	
		return result;		
	}
	
public static Mhrmattendance[] getAttendanceType(int C_BPartner_ID, Timestamp sdt, Timestamp edt, String atype) {
		
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmattendance result = null;
					
		ArrayList<Mhrmattendance> list = new ArrayList<Mhrmattendance>();
		// 
			String sql_ = "SELECT *	FROM HRM_ATTENDANCE \n" +
							"WHERE ISACTIVE = 'Y' \n" +
								"AND AD_CLIENT_ID ="  + Env.getAD_Client_ID(Env.getCtx()) + " \n" +
								"AND C_BPARTNER_ID =" + C_BPartner_ID   + " \n" +
								"AND STARTDATE >="    + DB.TO_DATE(sdt) + " \n" +
								"AND ENDDATE   <="    + DB.TO_DATE(edt) + " \n" +
								"AND HRM_ATTENDANCETYPE ='" + atype + "' \n";
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				while(rs.next()){
					result = new  Mhrmattendance(Env.getCtx(), rs, null);
					list.add(result);
				}
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}	
			
			return list.toArray(new Mhrmattendance[list.size()]);		
	}
}
